var http = require('../../script/api.js');
var app = getApp();
var config = require('../../script/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailPopShow: false,
    dataList: [],
    StartDate: '',
    EndDate: '',
    TotalCount: {
      WeiJieShou: 0,
      ChuliZHong: 0,
      WeiShouLi: 0
    },
    animationData: {},
    DetailCount: {
      Title: '',
      AllCount: 0,
      WeiJieShou: 0,
      ChuliZHong: 0,
      YiWanGong: 0,
      TuiHuiZhong: 0,
      ZhongZhiZhong: 0,
      YiTuiHui: 0,
      ZhongZhi: 0,
      WxAllCount: 0,
      WxWeiShouLi: 0,
      WxYiShouLi: 0,
      WxBuShouLi: 0,
    },
    // click: false, //是否显示弹窗内容
    // option: false, //显示弹窗或关闭弹窗的操作动画
    animationData: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserInfo()
    this.getDate();
    this.GetWxReportOrderCount();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    // var animation = this.createAnimation();
    // //先隐藏内容
    // animation.translateY(400).step()
    // this.setData({
    //   animationData: animation.export()
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  _refreshData: function () {
    this.setData({ //初始化数据
      dataList: [],
      detailPopShow: false,
      TotalCount: {
        WeiJieShou: 0,
        ChuliZHong: 0,
        WeiShouLi: 0
      },
      DetailCount: {
        Title: '',
        AllCount: 0,
        ChongFa: 0,
        WeiJieShou: 0,
        ChuliZHong: 0,
        YiWanGong: 0,
        TuiHuiZhong: 0,
        ZhongZhiZhong: 0,
        YiTuiHui: 0,
        ZhongZhi: 0,
        WxAllCount: 0,
        WxWeiShouLi: 0,
        WxYiShouLi: 0,
        WxBuShouLi: 0,
      },
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    this._refreshData();
    this.GetWxReportOrderCount();
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  createAnimation: function () {  
    return wx.createAnimation({  
      duration: 300, // 动画持续时间  
      timingFunction: 'ease', // 定义动画的效果  
    });
  }, 
  getUserInfo: function () {
    try {
      var value = wx.getStorageSync(config.storageKey.userinfo)
      if (value) {
        this.setData({
          userinfo: value
        })
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  getDate: function () {
    let startDate = new Date();
    let endDate = new Date();
    startDate.setTime(startDate.getTime() - 3600 * 1000 * 24)
    var year1 = this.withData(startDate.getFullYear());
    var month1 = this.withData(startDate.getMonth() + 1);
    var day1 = this.withData(startDate.getDate());
    var year2 = this.withData(endDate.getFullYear());
    var month2 = this.withData(endDate.getMonth() + 1);
    var day2 = this.withData(endDate.getDate());
    const StartDate = year1 + '-' + month1 + '-' + day1 + ' 00:00:00';
    const EndDate = year2 + '-' + month2 + '-' + day2 + ' 23:59:59'
    this.setData({
      StartDate,
      EndDate
    })
  },
  withData: function (param) {
    return param < 10 ? '0' + param : '' + param;
  },
  GetWxReportOrderCount: function () {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let params = {
      CreaterBeginTime: this.data.StartDate,
      CreaterEndTime: this.data.EndDate
    };
    http.GetWxReportOrderCount(params).then(res => {
      if (res.code === 200) {
        const resData = res.data;
        const dataList = resData.TreeData || [];
        this.setData({
          dataList,
          'TotalCount.WeiJieShou': resData.OrderCount.WeiJieShou,
          'TotalCount.ChuliZHong': resData.OrderCount.ChuliZHong,
          'TotalCount.WeiShouLi': resData.WxLogCount.WeiShouLi,
        })
      } else {
        wx.showToast({
          icon: 'none',
          title: res.info,
          duration: config.toast.duration
        })
      }
      wx.hideLoading();
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  toggleShow: function (e) {
    const index = e.currentTarget.dataset.index + ''
    const indexs = index.split('-');
    if (indexs.length === 1) {
      this.data.dataList[indexs[0]].expand = !this.data.dataList[indexs[0]].expand
    } else if (indexs.length === 2) {
      this.data.dataList[indexs[0]].ChildNodeList[indexs[1]].expand = !this.data.dataList[indexs[0]].ChildNodeList[indexs[1]].expand
    } else if (indexs.length === 3) {
      this.data.dataList[indexs[0]].ChildNodeList[indexs[1]].ChildNodeList[indexs[2]].expand = !this.data.dataList[indexs[0]].ChildNodeList[indexs[1]].ChildNodeList[indexs[2]].expand
    }
    // this.data.dataList[index].expand = !this.data.dataList[index].expand
    this.setData({
      dataList: this.data.dataList
    });
  },
  toDetalis: function (e) {
    const item = e.currentTarget.dataset.item;
    //显示底部弹出层
    var animation = this.createAnimation();
    animation.translateY(0).step()
    this.setData({
      animationData: animation.export(),
      detailPopShow: true,
      // click: true,
      // option: true,
      'DetailCount.Title': item.DeptName,
      'DetailCount.AllCount': item.AllCount,
      'DetailCount.ChongFa': item.ChongFa,
      'DetailCount.WeiJieShou': item.WeiJieShou,
      'DetailCount.ChuliZHong': item.ChuliZHong,
      'DetailCount.YiWanGong': item.YiWanGong,
      'DetailCount.TuiHuiZhong': item.TuiHuiZhong,
      'DetailCount.ZhongZhiZhong': item.ZhongZhiZhong,
      'DetailCount.YiTuiHui': item.YiTuiHui,
      'DetailCount.ZhongZhi': item.ZhongZhi,
      'DetailCount.WxAllCount': item.WxAllCount,
      'DetailCount.WxWeiShouLi': item.WxWeiShouLi,
      'DetailCount.WxYiShouLi': item.WxYiShouLi,
      'DetailCount.WxBuShouLi': item.WxBuShouLi,
    });
  },
  closePop: function (e) {
    // this.setData({
    //   details: null,
    //   option: false,
    // })
    // setTimeout(() => {
    //   this.setData({
    //     click: false,
    //   })
    // }, 300)

    this.setData({
      detailPopShow: false,
      details: null,
    })
    var animation = this.createAnimation();
    animation.translateY(400).step()
    this.setData({
      animationData: animation.export() //导出动画api , 页面执行动画
    });
  },
  // 这个函数内容为空 用于阻止屏幕滚动
  preventTouchMove: function (e) {},
  changeBeginTime: function (e) {
    this.setData({
      StartDate: e.detail.currentDateTimeStr
    });
    if (this.data.EndDate !== '' && (new Date(this.data.EndDate).getTime() < new Date(this.data.StartDate).getTime())) {
      wx.showToast({
        icon: 'none',
        title: '开始日期不能大于结束日期，请重新选择',
        duration: 4000
      });
      return;
    }
    this.GetWxReportOrderCount();
  },
  changeEndTime: function (e) {
    this.setData({
      EndDate: e.detail.currentDateTimeStr
    });
    if (this.data.StartDate !== '' && (new Date(this.data.StartDate).getTime() > new Date(this.data.EndDate).getTime())) {
      wx.showToast({
        icon: 'none',
        title: '结束日期不能小于开始日期，请重新选择',
        duration: 4000
      });
      return;
    }
    this.GetWxReportOrderCount();
  },
})